package com.manager.plat.action;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IConfigLowerPartnerDao;
import com.manager.plat.dao.IConfigRouterDao;
import com.manager.plat.dao.IConversionDao;
import com.manager.plat.vo.AnalysisVo;
import com.manager.plat.vo.ConversionVo;
import com.manager.plat.vo.HangupRateVo;
import com.manager.plat.vo.TermVo;
import com.manager.plat.vo.UserCallinVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class ConversionPartnerAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8248813597911861306L;
	private static Logger logger = Logger.getLogger(ConversionPartnerAction.class);
	private IConversionDao conversionDao;
	private IConfigRouterDao configRouterDao;			//路由信息数据接口
	private IConfigLowerPartnerDao configLowerPartnerDao;//渠道信息数据接口
	private List<ConversionVo> conversionVoList;		//转化率信息VO列表
	private List<HangupRateVo> hangupRateVoList;		//挂断率信息VO列表
	private List<AnalysisVo> analysisVoList;			//数据分析信息VO
	private List<UserCallinVo> userCallinVoList;		//用户呼入信息VO
	private List<String> spnumberList;					//长号码信息列表
	private TermVo term;								//查询条件VO
	private HangupRateVo hangupRateVo; 					//挂断率信息VO
	
	public String getConversion(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		/**获得当前渠道路由列表*/
		spnumberList = configRouterDao.getSpnumberListLowerByUserId(userInfoVo.getId());
		
		term = null;
		conversionVoList = null;
		
		return SUCCESS;
	}
	public String getAllConversion(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		/**获得渠道用户信息*/
		int user_id = userInfoVo.getId();
		/**获得业务号码*/
		String sp_number = term.getSp_number();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		/**获得结束时间*/
		String time_end = term.getTime_end();
		/**根据渠道ID获得渠道长号码列表*/
		spnumberList = configRouterDao.getSpnumberListLowerByUserId(user_id);
		/**根据查询条件获得转化率列表*/
		conversionVoList = conversionDao.getConversionPartner(user_id, sp_number, time_begin, time_end);
		
		if(conversionVoList != null && conversionVoList.size() > 0){
			logger.info(String.format("获得转化率列表大小[%d]", conversionVoList.size()));
			Map<String, ConversionVo> conversionVoMap = new LinkedHashMap<String, ConversionVo>();
			for(ConversionVo conversionVo : conversionVoList){
				String sp_number_temp = conversionVo.getSp_number();
				String add_date = conversionVo.getAdd_date();
				String key = sp_number_temp + add_date;
				if(conversionVoMap.get(key) != null){
					int user_num = conversionVoMap.get(key).getUser_num();
					conversionVoMap.get(key).setUser_num(user_num + conversionVo.getFee_number());
					
					int lower_id_temp = conversionVo.getGroup_lower_id();
					if(lower_id_temp == 0){
						conversionVoMap.get(key).setNo_fee_number(conversionVo.getFee_number());
					}else{
						conversionVoMap.get(key).setFee_number(conversionVo.getFee_number());
					}
				}else{
					conversionVo.setUser_num(conversionVo.getFee_number());
					int lower_id_temp = conversionVo.getGroup_lower_id();
					if(lower_id_temp == 0){
						conversionVo.setNo_fee_number(conversionVo.getFee_number());
						conversionVo.setFee_number(0);
					}
					conversionVoMap.put(key, conversionVo);
				}
			}
			logger.info(String.format("完成转化率列表组合,大小[%d],开始遍历!", conversionVoMap.size()));
			
			/**遍历Map并计算转化率*/
			if(conversionVoMap != null && conversionVoMap.size() > 0){
				if(conversionVoList != null){
					conversionVoList.clear();
				}
				for(ConversionVo conversionVo : conversionVoMap.values()){
					double rate = convDivide(conversionVo.getFee_number(), conversionVo.getUser_num());
					conversionVo.setRate(rate);
					conversionVoList.add(conversionVo);
				}
			}
		}else{
			logger.info("获得转化率列表为空");
		}
		return SUCCESS;
	}
	
	public String getDetailConversion(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		/**获得渠道用户信息*/
		int user_id = userInfoVo.getId();
		/**获得业务号码*/
		String sp_number = term.getSp_number();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		/**获得结束时间*/
		String time_end = term.getTime_end();
		/**根据渠道ID获得渠道长号码列表*/
		spnumberList = configRouterDao.getSpnumberListLowerByUserId(user_id);
		/**根据查询条件获得转化率列表*/
		conversionVoList = conversionDao.getDetailConversionPartner(user_id, sp_number, time_begin, time_end);
		
		if(conversionVoList != null && conversionVoList.size() > 0){
			logger.info(String.format("获得转化率列表大小[%d]", conversionVoList.size()));
			Map<String, ConversionVo> conversionVoMap = new LinkedHashMap<String, ConversionVo>();
			for(ConversionVo conversionVo : conversionVoList){
				String sp_number_temp = conversionVo.getSp_number();
				String add_date = conversionVo.getAdd_date();
				String key = sp_number_temp + add_date;
				if(conversionVoMap.get(key) != null){
					int user_num = conversionVoMap.get(key).getUser_num();
					conversionVoMap.get(key).setUser_num(user_num + conversionVo.getFee_number());
					
					int lower_id_temp = conversionVo.getGroup_lower_id();
					if(lower_id_temp == 0){
						conversionVoMap.get(key).setNo_fee_number(conversionVo.getFee_number());
					}else{
						conversionVoMap.get(key).setFee_number(conversionVo.getFee_number());
					}
				}else{
					conversionVo.setUser_num(conversionVo.getFee_number());
					int lower_id_temp = conversionVo.getGroup_lower_id();
					if(lower_id_temp == 0){
						conversionVo.setNo_fee_number(conversionVo.getFee_number());
						conversionVo.setFee_number(0);
					}
					conversionVoMap.put(key, conversionVo);
				}
			}
			logger.info(String.format("完成转化率列表组合,大小[%d],开始遍历!", conversionVoMap.size()));
			
			/**遍历Map并计算转化率*/
			if(conversionVoMap != null && conversionVoMap.size() > 0){
				if(conversionVoList != null){
					conversionVoList.clear();
				}
				for(ConversionVo conversionVo : conversionVoMap.values()){
					double rate = convDivide(conversionVo.getFee_number(), conversionVo.getUser_num());
					conversionVo.setRate(rate);
					conversionVoList.add(conversionVo);
				}
			}
		}else{
			logger.info("获得转化率列表为空");
		}
		return SUCCESS;
	}
	
	public String getUserCallin(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		/**获得渠道用户信息*/
		int user_id = userInfoVo.getId();
		/**获得业务号码*/
		String sp_number = term.getSp_number();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		/**获得结束时间*/
		String time_end = term.getTime_end();
		/**获得详情时间*/
		String add_date= term.getAdd_date();
		userCallinVoList = conversionDao.getUserCallinPartner(user_id, sp_number, time_begin, time_end, add_date);
		return SUCCESS;
	}
	
	public String getProvince(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		/**获得渠道用户信息*/
		int user_id = userInfoVo.getId();
		/**获得业务号码*/
		String sp_number = term.getSp_number();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		/**获得结束时间*/
		String time_end = term.getTime_end();
		/**获得详情时间*/
		String add_date= term.getAdd_date();
		
		analysisVoList = conversionDao.getProvinceAnalysisPartner(sp_number, user_id, time_begin, time_end, add_date);
		if(analysisVoList != null && analysisVoList.size() > 0){
			Map<String, AnalysisVo> analysisVoListMap = new HashMap<String, AnalysisVo>();
			for(AnalysisVo analysisVo : analysisVoList){
				/**获得省份*/
				String province = analysisVo.getProvince();
				/**获得是否过滤*/
				int isfilter = analysisVo.getIsfilter();
				if(analysisVoListMap.get(province) != null){
					analysisVo.setFee_user_num_sum(analysisVo.getFee_user_num_sum() + analysisVo.getFee_user_num());
					analysisVo.setFee_minute_sum(analysisVo.getFee_minute_sum() + analysisVo.getFee_minute());
					analysisVo.setFee_price_sum(analysisVo.getFee_price_sum() + analysisVo.getFee_price());
					
					if(isfilter == 0){
						analysisVoListMap.get(province).setFee_user_num(analysisVo.getFee_user_num());
						analysisVoListMap.get(province).setFee_minute(analysisVo.getFee_minute());
						analysisVoListMap.get(province).setFee_price(analysisVo.getFee_price());
					}else{
						analysisVoListMap.get(province).setFee_user_num_filter(analysisVo.getFee_user_num());
						analysisVoListMap.get(province).setFee_minute_filter(analysisVo.getFee_minute());
						analysisVoListMap.get(province).setFee_price_filter(analysisVo.getFee_price());
					}
				}else{
					analysisVo.setFee_user_num_sum(analysisVo.getFee_user_num());
					analysisVo.setFee_minute_sum(analysisVo.getFee_minute());
					analysisVo.setFee_price_sum(analysisVo.getFee_price());
					if(isfilter == 1){
						analysisVo.setFee_user_num(0);
						analysisVo.setFee_user_num_filter(analysisVo.getFee_user_num());
						analysisVo.setFee_minute(0);
						analysisVo.setFee_minute_filter(analysisVo.getFee_minute());
						analysisVo.setFee_price(0);
						analysisVo.setFee_price_filter(analysisVo.getFee_price());
					}
					analysisVoListMap.put(province, analysisVo);
				}
			}
			
			if(analysisVoListMap != null && analysisVoListMap.size() > 0){
				analysisVoList.clear();
				for(AnalysisVo analysisVo : analysisVoListMap.values()){
					analysisVoList.add(analysisVo);
				}
			}
		}
		
		return SUCCESS;
	}
	
	public String getHangup(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		/**获得当前渠道路由列表*/
		spnumberList = configRouterDao.getSpnumberListLowerByUserId(userInfoVo.getId());
		
		term = null;
		hangupRateVoList = null;
		
		return SUCCESS;
	}
	
	public String getHangupList(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		int user_id = userInfoVo.getId();
		
		/**获得业务号码*/
		String sp_number = term.getSp_number();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		
		hangupRateVoList = conversionDao.getHangupRateListPartner(sp_number, time_begin, user_id);
		if(hangupRateVoList != null && hangupRateVoList.size() > 0){
			for(int i = 0; i < hangupRateVoList.size(); i++){
				int prv_five = hangupRateVoList.get(i).getPrv_five();
				int prv_ten = hangupRateVoList.get(i).getPrv_ten();
				int prv_fifty = hangupRateVoList.get(i).getPrv_fifty();
				
				double prv_five_rate = convDivide(prv_five, prv_fifty);
				double prv_ten_rate = convDivide(prv_ten, prv_fifty);
				hangupRateVoList.get(i).setPrv_five_rate(prv_five_rate);
				hangupRateVoList.get(i).setPrv_ten_rate(prv_ten_rate);
			}
		}
		
		return SUCCESS;
	}
	
	public String getProvinceHangup(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		int user_id = userInfoVo.getId();
		
		
		/**获得业务号码*/
		String sp_number = term.getSp_number();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		hangupRateVoList = conversionDao.getProvinceHangupRateVoPartner(sp_number, time_begin, user_id);
		if(hangupRateVoList != null && hangupRateVoList.size() > 0){
			for(int i = 0; i < hangupRateVoList.size(); i++){
				int prv_five = hangupRateVoList.get(i).getPrv_five();
				int prv_ten = hangupRateVoList.get(i).getPrv_ten();
				int prv_fifty = hangupRateVoList.get(i).getPrv_fifty();
				
				double prv_five_rate = convDivide(prv_five, prv_fifty);
				double prv_ten_rate = convDivide(prv_ten, prv_fifty);
				hangupRateVoList.get(i).setPrv_five_rate(prv_five_rate);
				hangupRateVoList.get(i).setPrv_ten_rate(prv_ten_rate);
			}
		}
		return SUCCESS;
	}
	
	public String getDetailHangup(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		int user_id = userInfoVo.getId();
		
		/**获得业务号码*/
		String sp_number = term.getSp_number();
		/**获得开始时间*/
		String time_begin = term.getTime_begin();
		hangupRateVoList = conversionDao.getDetailHangupRateVoPartner(sp_number, time_begin, user_id);
		
		return SUCCESS;
	}
	
	
	private double convDivide(int numerator, int denominator){
        if(denominator != 0){
            return new BigDecimal(numerator).multiply(new BigDecimal(100)).divide(new BigDecimal(denominator), 2, BigDecimal.ROUND_HALF_UP).doubleValue();
        }
        return 0;
    }
	
	public IConversionDao getConversionDao() {
		return conversionDao;
	}
	public void setConversionDao(IConversionDao conversionDao) {
		this.conversionDao = conversionDao;
	}
	public IConfigRouterDao getConfigRouterDao() {
		return configRouterDao;
	}
	public void setConfigRouterDao(IConfigRouterDao configRouterDao) {
		this.configRouterDao = configRouterDao;
	}
	public IConfigLowerPartnerDao getConfigLowerPartnerDao() {
		return configLowerPartnerDao;
	}
	public void setConfigLowerPartnerDao(IConfigLowerPartnerDao configLowerPartnerDao) {
		this.configLowerPartnerDao = configLowerPartnerDao;
	}
	public List<ConversionVo> getConversionVoList() {
		return conversionVoList;
	}
	public void setConversionVoList(List<ConversionVo> conversionVoList) {
		this.conversionVoList = conversionVoList;
	}
	public List<HangupRateVo> getHangupRateVoList() {
		return hangupRateVoList;
	}
	public void setHangupRateVoList(List<HangupRateVo> hangupRateVoList) {
		this.hangupRateVoList = hangupRateVoList;
	}
	public List<AnalysisVo> getAnalysisVoList() {
		return analysisVoList;
	}
	public void setAnalysisVoList(List<AnalysisVo> analysisVoList) {
		this.analysisVoList = analysisVoList;
	}
	public List<UserCallinVo> getUserCallinVoList() {
		return userCallinVoList;
	}
	public void setUserCallinVoList(List<UserCallinVo> userCallinVoList) {
		this.userCallinVoList = userCallinVoList;
	}
	public List<String> getSpnumberList() {
		return spnumberList;
	}
	public void setSpnumberList(List<String> spnumberList) {
		this.spnumberList = spnumberList;
	}
	public TermVo getTerm() {
		return term;
	}
	public void setTerm(TermVo term) {
		this.term = term;
	}
	public HangupRateVo getHangupRateVo() {
		return hangupRateVo;
	}
	public void setHangupRateVo(HangupRateVo hangupRateVo) {
		this.hangupRateVo = hangupRateVo;
	}
}
